//
//  NIDropDown.m
//  NIDropDown
//
//  Created by Bijesh N on 12/28/12.
//  Copyright (c) 2012 Nitor Infotech. All rights reserved.
//

#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"

@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UIButton *btnSender;
//@property(nonatomic, strong) NSArray *list;
@property(nonatomic, assign) CGFloat height;
@property(nonatomic, assign) CGRect orginFrame;
@property(nonatomic, strong) UIView *bindView;
@property(nonatomic, assign) NSInteger curColumn;
//@property(nonatomic, assign) CGFloat cellheight;
//@property(nonatomic, strong) UIFont *font;

@end

@implementation NIDropDown
@synthesize table;
@synthesize btnSender;
//@synthesize list;
@synthesize delegate;
@synthesize cellBankGroundColor;

-(id)initDropDown:(UIView *)bindingView X:(CGFloat)x Y:(CGFloat)y Height:(CGFloat)menuHeight
{
    self = [super init];
    if (self)
    {
        self.bindView = bindingView;
        self.orginFrame = CGRectMake(x, y,
                                     bindingView.frame.size.width,
                                     bindingView.frame.size.height);
        
        self.height = menuHeight;
//        self.cellheight = cellHeight;
//        self.font = font;
        
        // Initialization code
        self.frame = CGRectMake(self.orginFrame.origin.x,
                                self.orginFrame.origin.y + self.orginFrame.size.height,
                                self.orginFrame.size.width, 0);
        NSLog(@"%f",self.frame.origin.x);
//        self.list = [NSArray arrayWithArray:arr];
//        self.layer.masksToBounds = NO;
//        self.layer.cornerRadius = 8;
//        self.layer.shadowOffset = CGSizeMake(-5, 5);
//        self.layer.shadowRadius = 5;
//        self.layer.shadowOpacity = 0.5;
        
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
                                                    self.orginFrame.size.width, 0)];
        table.delegate = self;
        table.dataSource = self;
        
        table.backgroundColor = [UIColor whiteColor];
        
//        table.layer.cornerRadius = 5;
//        table.backgroundColor = [UIColor colorWithRed:234/255.0 green:234/255.0
//                                                 blue:234/255.0 alpha:1];
        
        table.separatorStyle = UITableViewCellSeparatorStyleNone;
//        table.separatorColor = [UIColor colorWithRed:234/255.0 green:234/255.0
//                                                blue:234/255.0 alpha:1];
        table.scrollEnabled = YES;
        
        UIEdgeInsets inset;
        inset.right = 0;
//        inset.left = -20;
        [table setSeparatorInset:inset];

        
        [self addSubview:table];
    }
    return self;
}

- (void)reloadData
{
    if (self.table)
    {
        [table reloadData];
    }
}

- (void)showDropDownOfColumn:(NSInteger)column
{
    self.curColumn = column;
    
    if (self)
    {
        // Initialization code
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        self.frame = CGRectMake(self.orginFrame.origin.x,
                                self.orginFrame.origin.y + self.orginFrame.size.height,
                                self.orginFrame.size.width,
                                self.height);
        
        table.frame = CGRectMake(0, 0, self.orginFrame.size.width, self.height);
        [UIView commitAnimations];
        
        [self.bindView.superview addSubview:self];
        
        [table reloadData];
    }
}

- (void)hideDropDown
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.frame = CGRectMake(self.orginFrame.origin.x,
                            self.orginFrame.origin.y + self.orginFrame.size.height,
                            self.orginFrame.size.width, 0);
    
    table.frame = CGRectMake(0, 0, self.orginFrame.size.width, 0);
    [UIView commitAnimations];
}

- (void)setMenuHeight:(CGFloat)height
{
    self.height = height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if ([self.delegate respondsToSelector:@selector(NIDropDownNumberOfSectionsInTableView:Column:)]) {
        
        return [self.delegate NIDropDownNumberOfSectionsInTableView:tableView Column:self.curColumn];
    }
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(NIDropDownTableView:heightForRowAtIndexPath:Column:)])
    {
        return [self.delegate NIDropDownTableView:tableView heightForRowAtIndexPath:indexPath Column:self.curColumn];
    }
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.delegate respondsToSelector:@selector(NIDropDownTableView:numberOfRowsInSection:Column:)])
    {
        return [self.delegate NIDropDownTableView:tableView numberOfRowsInSection:section Column:self.curColumn];
    }
    
    return 0;
}   

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(NIDropDownTableView:cellForRowAtIndexPath:Column:)])
    {
        return [self.delegate NIDropDownTableView:tableView cellForRowAtIndexPath:indexPath Column:self.curColumn];
    }
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;

    }
    cell.textLabel.textColor = [UIColor blackColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    [self hideDropDown];
    
    if ([self.delegate respondsToSelector:@selector(NIDropDownTableView:didSelectRowAtIndexPath:Column:)])
    {
        [self.delegate NIDropDownTableView:tableView didSelectRowAtIndexPath:indexPath Column:self.curColumn];
    }
}

@end


















